package com.hanbang.gycatering.aboutme;

import java.io.File;
import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.classify.ClassifyActivity;
import com.hanbang.gycatering.comeon.LoginActivity;
import com.hanbang.gycatering.homepage.MainBaseActivity;
import com.hanbang.gycatering.homepage.HomePageActivity;
import com.hanbang.gycatering.myviews.CircleImageView;
import com.hanbang.gycatering.myviews.MyDialog;
import com.hanbang.gycatering.myviews.MyDialog.MODE;
import com.hanbang.gycatering.myviews.MyDialog.OnClickCallback;
import com.hanbang.gycatering.order.MyOrder;
import com.hanbang.gycatering.property.FixRecord;
import com.hanbang.gycatering.property.PropertyService;
import com.hanbang.gycatering.property.PropertyfeeRecord;
import com.hanbang.gycatering.shoppingcart.ShoppingCart;
import com.hanbang.gycatering.utils.DbUtil;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MySharedPreferences;
import com.hanbang.gycatering.utils.MyToast;
import com.hanbang.gycatering.utils.ShareMethod;
import com.hanbang.gycatering.utils.SystemTools;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/*
 * 
 * 
 */
public class AboutMe extends MainBaseActivity {
	private LinearLayout ll_one, ll_two, ll_three, ll_four, ll_five,
			ll_feedback, ll_mycollection, ll_myorder, ll_myfix, ll_mypayment,ll_myaddress;
	private CircleImageView my_head;
	private TextView my_name, button_eixt,my_balance,add_money;
	// 带用户名的路径

	// 拍完照存放的名称
	private String camera = "capture.jpg";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aboutme);
		initView();
		setTheClick();
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("关于我");
		((ImageView) findViewById(R.id.top_bar_left_image))
				.setVisibility(View.GONE);
		ll_one = (LinearLayout) findViewById(R.id.bottom_bar_ll1);
		ll_two = (LinearLayout) findViewById(R.id.bottom_bar_ll2);
		ll_three = (LinearLayout) findViewById(R.id.bottom_bar_ll3);
		ll_four = (LinearLayout) findViewById(R.id.bottom_bar_ll4);
		ll_five = (LinearLayout) findViewById(R.id.bottom_bar_ll5);
		ll_feedback = (LinearLayout) findViewById(R.id.ll_feedback);
		ll_mycollection = (LinearLayout) findViewById(R.id.ll_mycollection);
		ll_mypayment = (LinearLayout) findViewById(R.id.ll_mypayment);
		ll_myaddress = (LinearLayout) findViewById(R.id.ll_myaddress);
		ll_myorder = (LinearLayout) findViewById(R.id.ll_myorder);
		ll_myfix = (LinearLayout) findViewById(R.id.ll_myfix);
		my_head = (CircleImageView) findViewById(R.id.my_head);
		my_name = (TextView) findViewById(R.id.my_name);
		my_balance = (TextView) findViewById(R.id.my_balance);
		add_money = (TextView) findViewById(R.id.add_money);
		button_eixt = (TextView) findViewById(R.id.text_button_eixt);
	}

	private void setTheClick() {
		ll_one.setOnClickListener(myOncClickListener);
		ll_two.setOnClickListener(myOncClickListener);
		ll_three.setOnClickListener(myOncClickListener);
		ll_four.setOnClickListener(myOncClickListener);
		ll_five.setOnClickListener(myOncClickListener);
		ll_feedback.setOnClickListener(myOncClickListener);
		ll_mycollection.setOnClickListener(myOncClickListener);
		ll_mypayment.setOnClickListener(myOncClickListener);
		ll_myaddress.setOnClickListener(myOncClickListener);
		ll_myorder.setOnClickListener(myOncClickListener);
		ll_myfix.setOnClickListener(myOncClickListener);
		my_head.setOnClickListener(myOncClickListener);
		my_name.setOnClickListener(myOncClickListener);
		add_money.setOnClickListener(myOncClickListener);
		button_eixt.setOnClickListener(myOncClickListener);
	}
	private OnClickListener myOncClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			final Intent intent = new Intent();
			switch (view.getId()) {
			case R.id.bottom_bar_ll1:
				intent.setClass(AboutMe.this, HomePageActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll2:
				intent.setClass(AboutMe.this, ClassifyActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll3:
				intent.setClass(AboutMe.this, PropertyService.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll4:
				intent.setClass(AboutMe.this, ShoppingCart.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.text_button_eixt:
				if(userData == null){
					MyToast.show(AboutMe.this, "您未登录账号", Toast.LENGTH_SHORT);
					break;
				}
				MyDialog dialog3 = new MyDialog(AboutMe.this,
						R.style.base_dialog, MODE.CONFIRM);
				dialog3.setTitle("注销账号");
				dialog3.setContent("确定退出当前账号？");
				dialog3.setOnClickCallback(new OnClickCallback() {

					@Override
					public void onClick(MyDialog dialog, Object number,
							Object content) {

						if ((Integer) number == 2) {
							// TODO Auto-generated method stub
							
							MySharedPreferences
									.setSharedPreferencesKeyAndValue(
											AboutMe.this,
											MySharedPreferences.USER_DATA,
											"user_id", -1);
							userData = null;
							intent.setClass(AboutMe.this, LoginActivity.class);
//							Intent intent =new Intent(AboutMe.this, LoginActivity.class);
							startActivity(intent);
						} else if ((Integer) number == 1) {

						}
					}
				});
				dialog3.show();
				break;
			case R.id.ll_feedback:
				if (isLogin(true)) {
				} else {
					break;
				}
				intent.setClass(AboutMe.this, FeedBack.class);
				startActivity(intent);
				break;
			case R.id.ll_mycollection:
				if (!isLogin(true)) {
					break;
				}
				intent.setClass(AboutMe.this, MyCollection.class);
				startActivity(intent);
				break;
			case R.id.ll_mypayment:
				if (!isLogin(true)) {
					break;
				}
				intent.setClass(AboutMe.this, PropertyfeeRecord.class);
				startActivity(intent);
				break;
			case R.id.ll_myaddress:
				if (!isLogin(true)) {
					break;
				}
				intent.setClass(AboutMe.this, AddressManage.class);
				startActivity(intent);
				break;
			case R.id.ll_myorder:
				if (!isLogin(true)) {
					break;
				}
				intent.setClass(AboutMe.this, MyOrder.class);
				startActivity(intent);
				break;
			case R.id.ll_myfix:
				if (!isLogin(true)) {
					break;
				}
				intent.setClass(AboutMe.this, FixRecord.class);
				startActivity(intent);
				break;
			case R.id.add_money:
				if (!isLogin(true)) {
					break;
				}
				intent.setClass(AboutMe.this, AddBalance.class);
				startActivity(intent);
				break;
			case R.id.my_head:
				if (isLogin(true)) {
				} else {
					break;
				}
				MyDialog dialog = new MyDialog(AboutMe.this,
						R.style.about_me_dialog, MODE.HEAD);
				dialog.setOnClickCallback(new OnClickCallback() {
					@Override
					public void onClick(MyDialog dialog, Object number,
							Object content) {
						// 拍照
						if ((Integer) number == 1) {
							File fDir = new File(MyApplication.picFilepath);
							if (fDir.exists() || fDir.mkdirs()) {
								File cameraFile = new File(fDir, camera);
								if (!cameraFile.exists()) {
									try {
										cameraFile.createNewFile();
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
								Intent intent = new Intent(
										MediaStore.ACTION_IMAGE_CAPTURE);
								intent.putExtra(MediaStore.EXTRA_OUTPUT,
										Uri.fromFile(cameraFile));
								startActivityForResult(intent, 998);
							}
							// 相册选择
						} else if ((Integer) number == 2) {
							// 获取相册
							Intent intent = new Intent();
							intent.setType("image/*");
							intent.setAction(Intent.ACTION_GET_CONTENT);
							intent.putExtra("return-data", true);
							startActivityForResult(intent, 999);
						}
					}
				});

				dialog.show();
				break;
			case R.id.my_name:
				if (!isLogin(true)) {
					break;
				}
				MyDialog dialog2 = new MyDialog(AboutMe.this,
						R.style.base_dialog, MODE.EDITTEXT);
				dialog2.setTitle("输入昵称");
				dialog2.setOnClickCallback(new OnClickCallback() {
					@Override
					public void onClick(MyDialog dialog, Object number,
							Object content) {
						if ((Integer) number == 2) {
							if (!TextUtils.isEmpty(String.valueOf(content)
									.trim())) {

								if (String.valueOf(content).trim()
										.equals(userData.getUsernick())) {
									MyToast.show(AboutMe.this, "您的昵称未改变",
											Toast.LENGTH_SHORT);
								} else {
									MyLog.e("hehehehhehehe",
											String.valueOf(content).trim()
													+ userData.getUsernick());
									changeName(String.valueOf(content).trim());
								}
							} else {
								MyToast.show(AboutMe.this, "请输入昵称",
										Toast.LENGTH_SHORT);
							}
						}
					}

				});
				dialog2.show();
				break;

			default:
				break;
			}
		}
	};

	// 修改用户名
	private void changeName(final String changename) {
		// TODO Auto-generated method stub
		MyLog.e("changeName",
				HttpInterfaces.name + "&userphone=" + userData.getUserphone()
						+ "&password=" + userData.getUserpassword()
						+ "&newname=" + changename);
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET,
				HttpInterfaces.name + "&userphone=" + userData.getUserphone()
						+ "&password=" + userData.getUserpassword()
						+ "&newname=" + changename,
				new MyRequestCallBack<String>(AboutMe.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								MyToast.show(AboutMe.this, "修改成功",
										Toast.LENGTH_SHORT);
								updataUserData("nickName", changename);
								readName();
							} else {
								MyToast.show(AboutMe.this, "获取数据失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(AboutMe.this, "获取数据失败",
									Toast.LENGTH_SHORT);
						}
					}
				});
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		/*
		 * 相册 resultCode: 正常返回-1 用户后退返回0
		 */

		if (requestCode == 999 && resultCode == -1) {
			Uri url = data.getData();
			Intent intent = new Intent(AboutMe.this, ClipPhotoActivity.class);
			intent.setData(url);
			intent.putExtra("type", 1);
			intent.putExtra("m", userData.getUserphone());
			intent.putExtra("p", userData.getUserpassword());
			startActivityForResult(intent, 1000);
		}
		/*
		 * 相片裁剪界面发过来的
		 */
		if (requestCode == 1000 && resultCode == 1000) {
			String p = data.getStringExtra("path");
			updataUserData("avatar", p);
			readTouxiang();
		}
		// 拍照返回的数据
		if (requestCode == 998 && resultCode == -1) {
			Intent intent = new Intent(AboutMe.this, ClipPhotoActivity.class);
			intent.putExtra("type", 2);
			intent.putExtra("m", userData.getUserphone());
			intent.putExtra("p", userData.getUserpassword());
			startActivityForResult(intent, 1000);
		}
		super.onActivityResult(requestCode, resultCode, data);
	};
	private void readTouxiang() {
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.morentx);
		if (userData != null) {
			if (userData.getAvatar() != null) {
				bitmapUtils.display(my_head,SystemTools.getHttpFileUrl(userData.getAvatar()));
			}
			else {
				my_head.setImageResource(R.drawable.morentx);
			}

		} else {
			my_head.setImageResource(R.drawable.morentx);
		}
	}

	private void readName() {
		if (userData != null) {
			my_name.setText(userData.getUsernick());
		} else {
			my_name.setText("点击登录");
		}
	}
	private void readBalance() {
		// TODO Auto-generated method stub
		if (userData != null) {
			my_balance.setText("￥"+userData.getBalanc());
			my_balance.setTextColor(getResources().getColor(R.color.red));
		} else {
			my_balance.setText("￥0.0");
			my_balance.setTextColor(getResources().getColor(R.color.green));
		}
		
	}

	private void updataUserData(String lie, String res) {

		if (userData != null) {
			try {
				if (lie.equals("avatar")) {
					userData.setAvatar(res);
				} else if (lie.equals("nickName")) {
					userData.setUsernick(res);
				}
				DbUtil.getDbUtils(this).update(userData, lie);
			} catch (DbException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		ShareMethod.setButton(ll_five, false);
		readTouxiang();
		readName();
		readBalance();

	};

	

	@Override
	protected void onPause() {
		super.onPause();
		ShareMethod.setButton(ll_five, true);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
